﻿////////////////////////////////////////////////////////////////////
//
//	Map_FVG C# Class Library.
//
//	Simbol
//	
//
//	autore: Gustavo Mainardi 
//	versione: 1.0
//	data: 01/12/2015
//
//
//	Cronologia delle versioni:
//
//	Versione 1.0 01/12/2015
//		versione originale
//
//////////////////////////////////////////////////////////////////////////
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SQLite;

namespace Map_Fvg
{
    public partial class Simbol : Form
    {
 
        private int oldPosX1, oldPosY1, oldPosX2, oldPosY2, oldPosX3, oldPosY3, oldPosX4, oldPosY4;
        private Size oldSize = new Size();
        private int RowSelected;

        public static string selectedLinea;
        static public int selectedType;

        public Simbol()
        {
            InitializeComponent();

            oldPosX1 = this.ClientSize.Width - button1.Location.X;
            oldPosY1 = this.ClientSize.Height - button1.Location.Y;

            oldPosX2 = this.ClientSize.Width - button2.Location.X;
            oldPosY2 = this.ClientSize.Height - button2.Location.Y;

            oldPosX3 = this.ClientSize.Width - button3.Location.X;
            oldPosY3 = this.ClientSize.Height - button3.Location.Y;

            oldPosX4 = this.ClientSize.Width - button4.Location.X;
            oldPosY4 = this.ClientSize.Height - button4.Location.Y;

            oldSize = this.Size;
        }


        private void Simbol_Load(object sender, EventArgs e)
        {

            if (selectedType == 0)          //  area
            {
                dataGridView1.ColumnCount = 7;
                this.dataGridView1.Columns[0].HeaderText = "codice area";
                this.dataGridView1.Columns[1].HeaderText = "denominazione";
                this.dataGridView1.Columns[2].HeaderText = "codice bordo";
                this.dataGridView1.Columns[3].HeaderText = "bordo";
                this.dataGridView1.Columns[4].HeaderText = "codice camp.";
                this.dataGridView1.Columns[5].HeaderText = "campitura";
                this.dataGridView1.Columns[6].HeaderText = "colore";

                this.dataGridView1.Columns[0].Width = 70;
                this.dataGridView1.Columns[1].Width = 300;
                this.dataGridView1.Columns[2].Width = 60;
                this.dataGridView1.Columns[3].Width = 150;
                this.dataGridView1.Columns[4].Width = 60;
                this.dataGridView1.Columns[5].Width = 150;
                this.dataGridView1.Columns[6].Width = 80;

                dataGridView1.AllowUserToAddRows = false;
                dataGridView1.AllowUserToDeleteRows = false;
                aggiorna_tabella_area();
            }

            if (selectedType == 1)          //  linea 
            {
                dataGridView1.ColumnCount = 6;
                this.dataGridView1.Columns[0].HeaderText = "codice linea";
                this.dataGridView1.Columns[1].HeaderText = "classe";
                this.dataGridView1.Columns[2].HeaderText = "denominazione";
                this.dataGridView1.Columns[3].HeaderText = "tipo";
                this.dataGridView1.Columns[4].HeaderText = "tratto";
                this.dataGridView1.Columns[5].HeaderText = "colore";

                this.dataGridView1.Columns[0].Width = 70;
                this.dataGridView1.Columns[1].Width = 100;
                this.dataGridView1.Columns[2].Width = 300;
                this.dataGridView1.Columns[3].Width = 80;
                this.dataGridView1.Columns[4].Width = 200;
                this.dataGridView1.Columns[5].Width = 80;


                dataGridView1.AllowUserToAddRows = false;
                dataGridView1.AllowUserToDeleteRows = false;
                aggiorna_tabella_linea();
            }

            if (selectedType == 2)          //  punto
            {
                dataGridView1.ColumnCount = 6;
                this.dataGridView1.Columns[0].HeaderText = "codice ";
                this.dataGridView1.Columns[1].HeaderText = "denominazione";
                this.dataGridView1.Columns[2].HeaderText = "codice simbolo";
                this.dataGridView1.Columns[3].HeaderText = "parametri";
                this.dataGridView1.Columns[4].HeaderText = "file .bmp";
                this.dataGridView1.Columns[5].HeaderText = "dimensione";
               
                this.dataGridView1.Columns[0].Width = 70;
                this.dataGridView1.Columns[1].Width = 400;
                this.dataGridView1.Columns[2].Width = 80;
                this.dataGridView1.Columns[3].Width = 150;
                this.dataGridView1.Columns[4].Width = 80;
                this.dataGridView1.Columns[5].Width = 80;

                dataGridView1.AllowUserToAddRows = false;
                dataGridView1.AllowUserToDeleteRows = false;
                aggiorna_tabella_punti();
                Size sz = dataGridView1.Size;

  //              MessageBox.Show(sz.ToString());
                sz.Width = 920;
                dataGridView1.Size = sz;
               
 //               MessageBox.Show("non ancora operativo");
            }

            if (selectedType == 3)          //  testo
            {
                dataGridView1.ColumnCount = 7;
                this.dataGridView1.Columns[0].HeaderText = "codice testo";
                this.dataGridView1.Columns[1].HeaderText = "denominazione";
                this.dataGridView1.Columns[2].HeaderText = "colore";
                this.dataGridView1.Columns[3].HeaderText = "font";
                this.dataGridView1.Columns[4].HeaderText = "altezza da tabelle";
                this.dataGridView1.Columns[5].HeaderText = "altezza";
                this.dataGridView1.Columns[6].HeaderText = "size";

                this.dataGridView1.Columns[0].Width = 70;
                this.dataGridView1.Columns[1].Width = 430;
                this.dataGridView1.Columns[2].Width = 80;
                this.dataGridView1.Columns[3].Width = 80;
                this.dataGridView1.Columns[4].Width = 80;
                this.dataGridView1.Columns[5].Width = 80;
                this.dataGridView1.Columns[6].Width = 80;

                dataGridView1.AllowUserToAddRows = false;
                dataGridView1.AllowUserToDeleteRows = false;
                aggiorna_tabella_testo();
            }
        }

        private SQLiteConnection SetConnection()
        {
            string DataSource = "Data Source=";
            DataSource = string.Concat(DataSource, FormIniz.cartellaSimb, ";");
            SQLiteConnection conn = new SQLiteConnection(DataSource);
            return (conn);

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //        aggiornamento
            if (RowSelected < 0)
            {
                MessageBox.Show("selezionare la linea che si vuole modificare");
                return;
            }

            if (selectedType == 0)
            {
                string s1 = dataGridView1[0, RowSelected].Value.ToString();
                selectedLinea = s1.Trim();

                infoDialog8 dlg = new infoDialog8();

                DialogResult dr = dlg.ShowDialog();
                if (dr == DialogResult.No) return;

                dataGridView1.RowCount = 0;
                aggiorna_tabella();
            }

            if (selectedType == 1)
            {
                string s1 = dataGridView1[0, RowSelected].Value.ToString();
                selectedLinea = s1.Trim();

                infoDialog5 dlg = new infoDialog5();

                DialogResult dr = dlg.ShowDialog();
                if (dr == DialogResult.No) return;

                dataGridView1.RowCount = 0;
                aggiorna_tabella();
            }

            if (selectedType == 2)      //  punti
            {
                string s1 = dataGridView1[0, RowSelected].Value.ToString();
                selectedLinea = s1.Trim();

                InfoPoint.init = 0;
                InfoPoint dlg = new InfoPoint();

                DialogResult dr = dlg.ShowDialog();
                if (dr == DialogResult.No) return;

                dataGridView1.RowCount = 0;
                aggiorna_tabella();
            }

            if (selectedType == 3)      //  testo
            {
                string s1 = dataGridView1[0, RowSelected].Value.ToString();
                selectedLinea = s1.Trim();

                infoDialog7 dlg = new infoDialog7();

                DialogResult dr = dlg.ShowDialog();
                if (dr == DialogResult.No) return;

                dataGridView1.RowCount = 0;
                aggiorna_tabella();
            }

        }

        private void Simbol_SizeChanged(object sender, EventArgs e)
        {
            Point pnt = new Point(this.ClientSize.Width - oldPosX1, this.ClientSize.Height - oldPosY1);
            button1.Location = pnt;
            Point pnt1 = new Point(this.ClientSize.Width - oldPosX2, this.ClientSize.Height - oldPosY2);
            button2.Location = pnt1;
            Point pnt2 = new Point(this.ClientSize.Width - oldPosX3, this.ClientSize.Height - oldPosY3);
            button3.Location = pnt2;
            Point pnt3 = new Point(this.ClientSize.Width - oldPosX4, this.ClientSize.Height - oldPosY4);
            button4.Location = pnt3;

            this.dataGridView1.Size += this.Size - oldSize; ;

            oldSize = this.Size;
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex == -1)
            {
                RowSelected = e.RowIndex;
            }
            else
            {
                RowSelected = -1;
            }

        }
//      inserimento nuovo codice
        private void button3_Click(object sender, EventArgs e)
        {
            if (selectedType == 0)
            {
                infoDialog9 dlg = new infoDialog9();

                DialogResult dr = dlg.ShowDialog();
                if (dr == DialogResult.No) return;

                dataGridView1.RowCount = 0;
                aggiorna_tabella();
            }

            if (selectedType == 1)
            {
                infoDialog4 dlg = new infoDialog4();

                DialogResult dr = dlg.ShowDialog();
                if (dr == DialogResult.No) return;

                dataGridView1.RowCount = 0;
                aggiorna_tabella();
            }

            if (selectedType == 2)
            {
                InfoPoint.init = 1;
                InfoPoint dlg = new InfoPoint();

                DialogResult dr = dlg.ShowDialog();
                if (dr == DialogResult.No) return;

                dataGridView1.RowCount = 0;
                aggiorna_tabella();
            }

            if (selectedType == 3)
            {
                infoDialog6 dlg = new infoDialog6();

                DialogResult dr = dlg.ShowDialog();
                if (dr == DialogResult.No) return;

                dataGridView1.RowCount = 0;
                aggiorna_tabella();
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {

            //      cancellazione
            if (RowSelected < 0)
            {
                MessageBox.Show("selezionare la riga che si vuole cancellare");
                return;
            }

            if (selectedType == 0)
            {
                string ss = "si vuole cancellare l'area ";
                string s1 = dataGridView1[0, RowSelected].Value.ToString();
                s1 = s1.Trim();
                ss = string.Concat(ss, s1, " ?");
                string caption = "cancellazione";
                var result = MessageBox.Show(ss, caption, MessageBoxButtons.YesNo,
                                     MessageBoxIcon.Question);


                if (result == DialogResult.No) return;

                using (SQLiteConnection conn = SetConnection())
                {
                    conn.Open();
                    using (SQLiteCommand cmd = new SQLiteCommand(conn))
                    {
                        ss = "delete from area where codice_area = '" + s1 + "'";

                        //             MessageBox.Show(ss);
                        cmd.CommandText = ss;
                        int n = cmd.ExecuteNonQuery();
                        if (n == 0) MessageBox.Show("cancellazione non eseguita");
                    }

                    conn.Close();
                }

                dataGridView1.RowCount = 0;
                aggiorna_tabella_area();
            }

            if (selectedType == 1)
            {
                string ss = "si vuole cancellare la linea ";
                string s1 = dataGridView1[0, RowSelected].Value.ToString();
                s1 = s1.Trim();
                ss = string.Concat(ss, s1, " ?");
                string caption = "cancellazione";
                var result = MessageBox.Show(ss, caption, MessageBoxButtons.YesNo,
                                     MessageBoxIcon.Question);


                if (result == DialogResult.No) return;

                using (SQLiteConnection conn = SetConnection())
                {
                    conn.Open();
                    using (SQLiteCommand cmd = new SQLiteCommand(conn))
                    {
                        ss = "delete from linea where codice_stclasse = '" + s1 + "'";

                        //             MessageBox.Show(ss);
                        cmd.CommandText = ss;
                        int n = cmd.ExecuteNonQuery();
                        if (n == 0) MessageBox.Show("cancellazione non eseguita");
                    }

                    conn.Close();
                }

                dataGridView1.RowCount = 0;
                aggiorna_tabella_linea();
            }

            if (selectedType == 2)
            {
                string ss = "si vuole cancellare il simbolo punto ";
                string s1 = dataGridView1[0, RowSelected].Value.ToString();
                s1 = s1.Trim();
                ss = string.Concat(ss, s1, " ?");
                string caption = "cancellazione";
                var result = MessageBox.Show(ss, caption, MessageBoxButtons.YesNo,
                                     MessageBoxIcon.Question);


                if (result == DialogResult.No) return;

                using (SQLiteConnection conn = SetConnection())
                {
                    conn.Open();
                    using (SQLiteCommand cmd = new SQLiteCommand(conn))
                    {
                        ss = "delete from point where codice_point = '" + s1 + "'";

                        //             MessageBox.Show(ss);
                        cmd.CommandText = ss;
                        int n = cmd.ExecuteNonQuery();
                        if (n == 0) MessageBox.Show("cancellazione non eseguita");
                    }

                    conn.Close();
                }
                dataGridView1.RowCount = 0;
                aggiorna_tabella_punti();

            }

            if (selectedType == 3)
            {
                string ss = "si vuole cancellare il testo ";
                string s1 = dataGridView1[0, RowSelected].Value.ToString();
                s1 = s1.Trim();
                ss = string.Concat(ss, s1, " ?");
                string caption = "cancellazione";
                var result = MessageBox.Show(ss, caption, MessageBoxButtons.YesNo,
                                     MessageBoxIcon.Question);


                if (result == DialogResult.No) return;

                using (SQLiteConnection conn = SetConnection())
                {
                    conn.Open();
                    using (SQLiteCommand cmd = new SQLiteCommand(conn))
                    {
                        ss = "delete from testo where codice_testo = '" + s1 + "'";

                        //             MessageBox.Show(ss);
                        cmd.CommandText = ss;
                        int n = cmd.ExecuteNonQuery();
                        if (n == 0) MessageBox.Show("cancellazione non eseguita");
                    }

                    conn.Close();
                }

                dataGridView1.RowCount = 0;
                aggiorna_tabella_testo();

            }
        }


        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            RowSelected = -1;
        }

        private void aggiorna_tabella()
        {
            if (selectedType == 0) aggiorna_tabella_area();

            if (selectedType == 1) aggiorna_tabella_linea();

            if (selectedType == 2) aggiorna_tabella_punti();

            if (selectedType == 3) aggiorna_tabella_testo();

        }

        private void aggiorna_tabella_linea()
        {

            int iRows = 0;
            using (SQLiteConnection conn = SetConnection())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {

                   cmd.CommandText = "select * from linea, classi where linea.codice_classe = classi.codice_classe";
     //                cmd.CommandText = "select * from linea";
                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            string[] row = new string[] {reader.GetString(0).ToString(), reader.GetString(7).ToString(),
                                reader.GetString(2).ToString(),reader.GetString(3).ToString(),reader.GetString(4).ToString(),
                                reader.GetString(5).ToString()};
                            dataGridView1.Rows.Add(row);

                            ++iRows;
                        }
                    }
                }
                conn.Close();
            }

            for (int i = 0; i < iRows; ++i)
            {
                string[] words;
                char[] delimiterChars = { ' ', ',' };

                string ss = dataGridView1[5, i].Value.ToString();

                words = ss.Split(delimiterChars);
                byte b1 = byte.Parse(words[0]);
                byte b2 = byte.Parse(words[1]);
                byte b3 = byte.Parse(words[2]);
                byte b4 = byte.Parse(words[3]);

                dataGridView1[5, i].Value = "";

                dataGridView1[5, i].Style.BackColor = Color.FromArgb(b1, b2, b3, b4);

            }
            textBox1.Text = iRows.ToString() + "  simboli linea";
            dataGridView1.Show();
        }

        private void aggiorna_tabella_testo()
        {

            int iRows = 0;
            using (SQLiteConnection conn = SetConnection())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {

                    cmd.CommandText = "select * from testo";

                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            string[] row = new string[] {reader.GetString(0).ToString(), reader.GetString(1).ToString(),
                                reader.GetString(3).ToString()};
                            dataGridView1.Rows.Add(row);
                            dataGridView1[3, iRows].Value = reader.GetString(2).ToString();
                            dataGridView1[4, iRows].Value = reader.GetString(4).ToString();

                            ++iRows;
                        }
                    }
                }
                conn.Close();
            }

            for (int i = 0; i < iRows; ++i)
            {
                string[] words;
                char[] delimiterChars = { ' ', ',' };

                string ss = dataGridView1[2, i].Value.ToString();

                words = ss.Split(delimiterChars);
                byte b1 = byte.Parse(words[0]);
                byte b2 = byte.Parse(words[1]);
                byte b3 = byte.Parse(words[2]);
                byte b4 = byte.Parse(words[3]);

                dataGridView1[2, i].Value = "";

                dataGridView1[2, i].Style.BackColor = Color.FromArgb(b1, b2, b3, b4);


                ss = dataGridView1[3, i].Value.ToString();

                Font ifont = stringToFont(ss);
                dataGridView1[5, i].Value = ifont.Height;
                dataGridView1[6, i].Value = ifont.Size;

            }
            textBox1.Text = iRows.ToString() + "  simboli testo";
            dataGridView1.Show();
        }

        private Font stringToFont(string ss)
        {
            float sizef = 1.0F;
            int ipos = ss.IndexOf("Size=", 0);
            if (ipos > 0)
            {
                int ipos1 = ss.IndexOf(",", ipos);
                int len = ipos1 - ipos - 5;
                string s1 = ss.Substring(ipos + 5, len);
                sizef = float.Parse(s1);
                //              MessageBox.Show(s1 + "  " + sizef.ToString()); 
            }




            Font ifont = new Font(ss, sizef);
            return (ifont);


        }

        private void aggiorna_tabella_area()
        {

            int iRows = 0;
            using (SQLiteConnection conn = SetConnection())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {

                    cmd.CommandText = "select * from area";

                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            string[] row = new string[] {reader.GetString(0).ToString(), reader.GetString(1).ToString(),
                                reader.GetString(4).ToString(), reader.GetString(5).ToString(),
                                reader.GetString(2).ToString(), reader.GetString(3).ToString(),
                            reader.GetString(6).ToString()
                            };
                            dataGridView1.Rows.Add(row);
                            //             dataGridView1[6, iRows].Value = reader.GetString(6).ToString();

                            ++iRows;
                        }
                    }
                }
                conn.Close();
            }

            for (int i = 0; i < iRows; ++i)
            {
                string[] words;
                char[] delimiterChars = { ' ', ',' };

                string ss = dataGridView1[6, i].Value.ToString();

                words = ss.Split(delimiterChars);
                byte b1 = byte.Parse(words[0]);
                byte b2 = byte.Parse(words[1]);
                byte b3 = byte.Parse(words[2]);
                byte b4 = byte.Parse(words[3]);

                dataGridView1[6, i].Value = "";

                dataGridView1[6, i].Style.BackColor = Color.FromArgb(b1, b2, b3, b4);


                //            ss = dataGridView1[4, i].Value.ToString();


            }
            textBox1.Text = iRows.ToString() + "  simboli area";
            dataGridView1.Show();
        }

        private void aggiorna_tabella_testo11()
        {

            int iRows = 0;
            using (SQLiteConnection conn = SetConnection())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {

                    cmd.CommandText = "select * from testo";

                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            string[] row = new string[] {reader.GetString(0).ToString(), reader.GetString(1).ToString(),
                                reader.GetString(2).ToString()};
                            dataGridView1.Rows.Add(row);
                            dataGridView1[3, iRows].Value = reader.GetString(3).ToString();
                            dataGridView1[4, iRows].Value = reader.GetString(4).ToString();

                            ++iRows;
                        }
                    }
                }
                conn.Close();
            }

            for (int i = 0; i < iRows; ++i)
            {
                string[] words;
                char[] delimiterChars = { ' ', ',' };

                string ss = dataGridView1[3, i].Value.ToString();

                words = ss.Split(delimiterChars);
                byte b1 = byte.Parse(words[0]);
                byte b2 = byte.Parse(words[1]);
                byte b3 = byte.Parse(words[2]);
                byte b4 = byte.Parse(words[3]);

                dataGridView1[3, i].Value = "";

                dataGridView1[3, i].Style.BackColor = Color.FromArgb(b1, b2, b3, b4);


                ss = dataGridView1[2, i].Value.ToString();

                Font ifont = stringToFont(ss);
                dataGridView1[5, i].Value = ifont.Height;
                dataGridView1[6, i].Value = ifont.Size;

            }
            textBox1.Text = iRows.ToString() + "  simboli testo";
            dataGridView1.Show();
        }

        private void aggiorna_tabella_punti()
        {
            int iRows = 0;
            using (SQLiteConnection conn = SetConnection())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {

                    cmd.CommandText = "select * from point";

                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            string[] row = new string[] {reader.GetString(0).ToString(), reader.GetString(1).ToString(),   
                                reader.GetString(2).ToString(),reader.GetString(3).ToString(),reader.GetString(4).ToString(),
                                reader.GetString(5).ToString()};
                            dataGridView1.Rows.Add(row);

                            ++iRows;
                        }
                    }
                }
                conn.Close();
            }
            textBox1.Text = iRows.ToString() + "  simboli punti";
            dataGridView1.Show();

        }

        private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
             if (e.ColumnIndex == -1)
             {
                  RowSelected = e.RowIndex;
             }
             else
             {
                  RowSelected = -1;
             }

             button2_Click(sender, e);

        }

    }

}


